package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import component.ComponentFactory;
import component.Structure;
import disk.Load;
import disk.Save;

public class SaveLoadPanel {
	private Structure structure;
	private ComponentFactory factory;
	private Box box;
	
	public SaveLoadPanel(Structure structure, ComponentFactory factory) {
		this.structure = structure;
		this.factory = factory;
		
		box = Box.createHorizontalBox();
		JButton save = new JButton("Save");
		box.add(save);
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				save();
			}
		});
		JButton load = new JButton("Load");
		box.add(load);
		load.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				load();
			}
		});
	}
	
	public Box getPanel() {
		return box;
	}
	
	private void save() {
		Save save = new Save(structure);
		try {
			save.save();
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(), "Error saving to disk", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void load() {
		Load load = new Load(structure, factory);
		try {
			load.load();
		} catch (IOException e) {			
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(), "Error loading from disk", JOptionPane.ERROR_MESSAGE);
		}
	}
}
